﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ParseData
{
    public class BPSensorReading : ISensorReading
    {
        private BPSensor sensor;

        private string _Date = "";
        /// <summary>Get or set the reading date.</summary>
        public string Date
        {
            get { return this._Date; }
            set { this._Date = value; }
        }

        private string _Hour = "";
        /// <summary>Get or set the reading hour.</summary>
        public string Hour
        {
            get { return this._Hour; }
            set { this._Hour = value; }
        }

        private double _ValueSys;
        /// <summary>Get or set the reading systolic value.</summary>
        public double ValueSys
        {
            get { return this._ValueSys; }
            set { this._ValueSys = value; }
        }

        private double _ValueDia;
        /// <summary>Get or set the reading diastolic value.</summary>
        public double ValueDia
        {
            get { return this._ValueDia; }
            set { this._ValueDia = value; }
        }

        /// <summary>Constructor.</summary>
        public BPSensorReading(BPSensor sensor)
        {
            this.sensor = sensor;
        }

        /// <summary>Constructor + value.</summary>
        public BPSensorReading(BPSensor sensor, double valueSys, double valueDia)
        {
            this.sensor = sensor;
            this.ValueSys = valueSys;
            this.ValueDia = valueDia;
        }

        public bool HasValue()
        {
            bool hasVal = false;
            if (_ValueSys > 0) hasVal = true;
            return hasVal;
        }

        /// <summary>Return values format.</summary>
        public string ReadableString
        {
            get
            {
                //String result = String.Format("{0} {1}", _ValueSys, _ValueDia);
                String result = _ValueSys.ToString("00.0") + " " + _ValueDia.ToString("00.0");
                if (_Date.Length != 0)
                    result += String.Format(" {0}", _Date);
                if (_Hour.Length != 0)
                    result += String.Format(" {0}", _Hour);
                return result;
            }
        }
    }
    /// <summary>
    /// Class to read data files with BP values of the desired type.
    /// </summary>
    public class BPSensor : ParseData.ISensor
    {
        private string[] fileLines;
        private string _SensorID;

        private double sumSys = 0;
        private double sumDia = 0;
        private int countSys = 0;
        private int countDia = 0;

        public ISensorReading AverageValue
        {
            get
            {
                if (countSys > 0 && countDia > 0)
                    return new BPSensorReading(this, sumSys / countSys, sumDia / countDia);
                else
                    return null;
            }
        }

        private double minSys = 0;
        private double minDia = 0;

        public ISensorReading MinValue
        {
            get
            {
                if (countSys > 0 && countDia > 0)
                    return new BPSensorReading(this, minSys, minDia);
                else
                    return null;
            }
        }

        private double maxSys = 0;
        private double maxDia = 0;
        public ISensorReading MaxValue
        {
            get
            {
                if (countSys > 0 && countDia > 0)
                    return new BPSensorReading(this, maxSys, maxDia);
                else
                    return null;
            }
        }

        public void ResetMinValue()
        {
            minSys = 0;
            minDia = 0;
        }
        public void ResetMaxValue()
        {
            maxSys = 0;
            maxDia = 0;
        }

        BPSensorReading lastReading;

        public string SenzorID
        {
            get { return this._SensorID; }
            set { this._SensorID = value; }
        }

        public string TypeName
        {
            get { return "BP sensor"; }
        }

         /// <param name="FileName">Location of the file.</param>
        public BPSensor(string Filename)
        {
            string fileLine = "";
            fileLines = FileReaderUtil.SplitFileByLine(Filename);

            // Sensor's ID
            int indexID = 1;
            {
                fileLine = fileLines[indexID].Trim();
                _SensorID = fileLine.Substring(0, 6).Trim();
            }
        }

        private int fileIndex = 3;


        /// <summary>
        /// Parse the given positional file.
        /// </summary>
        public ISensorReading ReadNewValue()
        {
            if (fileIndex < fileLines.Length)
            {
                string fileLine = fileLines[fileIndex].Trim();
                lastReading = new BPSensorReading(this);

                if (fileLine.Length > 0)
                {
                    lastReading.ValueSys = Double.Parse(fileLine.Substring(0, 5).Trim(), new System.Globalization.CultureInfo("en-US"));
                    lastReading.ValueDia = Double.Parse(fileLine.Substring(5, 6).Trim(), new System.Globalization.CultureInfo("en-US"));
                    lastReading.Date = fileLine.Substring(11, 11).Trim();
                    lastReading.Hour = fileLine.Substring(22, 8).Trim();
                }

                double curentSenzorValSys = lastReading.ValueSys;
                double curentSenzorValDia = lastReading.ValueDia;

                //BP caluclations
                //Systolic pressure calculations
                // min Systolic      
                if (curentSenzorValSys < 110) minSys = curentSenzorValSys;
                // max Systolic
                if (curentSenzorValSys > 130) maxSys = curentSenzorValSys;
                // average Systolic
                sumSys = sumSys + curentSenzorValSys;
                countSys++;
                double tempMed = sumSys / countSys;

                //Diastolic pressure calculations
                // min Diastolic      
                if (curentSenzorValDia < 75) minDia = curentSenzorValDia;
                // max Diastolic   
                if (curentSenzorValDia > 85) maxDia = curentSenzorValDia;
                // average Diastolic  
                sumDia = sumDia + curentSenzorValDia;
                countDia++;
                double tempMed1 = sumDia / countDia;
                fileIndex++;
            }
             else
             {
                 lastReading = null;
             }

             return lastReading;
        }
    }
}